<template>
  <view class="detail-view">
    <h1>{{ title }}</h1>
    <view class="order-details">
      <view class="detail-item">
        <span class="label">项目名称：</span>
        <span class="value">{{ projectInfo.gym_name || '未选择' }}</span>
      </view>
      <view class="detail-item">
        <span class="label">地点：</span>
        <span class="value">{{ projectInfo.address || '未提供' }}</span>
      </view>
      <view class="detail-item">
        <span class="label">描述：</span>
        <span class="value">{{ projectInfo.description || '无描述' }}</span>
      </view>
      <view class="detail-item">
        <span class="label">预约日期：</span>
        <span class="value">{{ formattedDate || '未选择' }}</span>
      </view>
      <view class="detail-item">
        <span class="label">选中时段：</span>
        <view class="time-slots">
          <view class="time-tag" v-for="(time, index) in formattedTimeSlots" :key="index">{{ time }}</view>
        </view>
      </view>
    </view>
    <view class="confirm-btn" @click="submitOrder">确认预约</view>
  </view>
</template>

<script>
  import {
    createorders
  } from "../../api/DetailView/DetailView";
  export default {
    data() {
      return {
        title: '预约详情',
        selectedTimes: [],
        projectInfo: {},
        date: ''
      }
    },
    computed: {
      formattedDate() {
        return this.date.replace(/-/g, '/');
      },
      // 更新formattedTimeSlots计算属性
      formattedTimeSlots() {
        // 直接返回时间段文本
        return this.selectedTimes.map(time => time.text);
      }
    },
    onLoad(options) {
      // console.log(options);

      // Parse parameters from URL
      this.projectInfo = {
        gym_id: options.gymId || '',
        gym_name: options.gymName || '',
        address: options.address || '',
        description: options.description || '',

      };


      if (options.timeSlots) {
        this.selectedTimes = options.timeSlots.split(',').map(item => {
          const [id, text] = item.split('|'); // 分割id和text
          return {
            id: id.trim(),
            text: text.trim()
          };
        });
      } else {
        this.selectedTimes = [];
      }

      // Set current date
      this.date = options.date || '';
    },
    methods: {
      submitOrder() {
        // 构造订单数据对象
        const orderData = {
          gymId: this.projectInfo.gym_id,
          gymName: this.projectInfo.gym_name,
          address: this.projectInfo.address,
          description: this.projectInfo.description,
          date: this.date,
          // 提交时只传递id列表
          timeSlotIds: this.selectedTimes.map(slot => slot.id).join(',')
        };
        // console.log(orderData);
        // 调用创建订单接口
        createorders(orderData)
          .then(response => {
            // console.log('创建订单成功:', response);
            // 接口调用成功处理
            uni.showToast({
              title: '预约成功',
              icon: 'success'
            });
            // 跳转到我的订单页面
            setTimeout(() => {
              uni.navigateTo({
                url: '/reservation/MyOrderView/MyOrderView'
              });
            }, 1500);
          })
          .catch(error => {
            // 接口调用失败处理
            uni.showToast({
              title: '预约失败，请重试',
              icon: 'none'
            });
            console.error('创建订单失败:', error);
          });
      }
    }
  }
</script>

<style scoped>
  .detail-view {
    padding: 20px;
    background-color: #f5f5f5;
    min-height: 100vh;
  }

  .order-details {
    background-color: #fff;
    border-radius: 10px;
    padding: 20px;
    margin: 20px 0;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  }

  .detail-item {
    display: flex;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
  }

  .detail-item:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
  }

  .label {
    width: 100px;
    color: #666;
    font-weight: bold;
  }

  .value {
    flex: 1;
    color: #333;
  }

  .time-slots {
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
  }

  .time-tag {
    background-color: #159B38;
    color: white;
    padding: 5px 10px;
    border-radius: 15px;
    font-size: 14px;
  }

  .confirm-btn {
    background-color: #159B38;
    color: white;
    text-align: center;
    padding: 15px;
    border-radius: 8px;
    margin-top: 20px;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  }
</style>